import Taro from "@tarojs/taro"

interface UseOrderReturn {
  // 收款账号类型
  handleReceiptType: (type: string) => string
  // 拉取小程序半屏模式
  openEmbeddedMiniProgram: (id: string) => void
}

/**
 * 用于处理订单的逻辑
 */
export const useOrder = (): UseOrderReturn => {
  // 收款账号类型
  const handleReceiptType = (type: 'WECHAT' | 'ALIPAY' | 'UNION_PAY') => {
    if (type === 'WECHAT') {
      return '微信'
    } else if (type === 'ALIPAY') {
      return '支付宝'
    } else if (type === 'UNION_PAY') {
      return '银行卡'
    }
    return '未知'
  }
  // 拉去小程序半屏模式】
  const openEmbeddedMiniProgram = (id: string) => {
    let openMiniProgram = Taro.navigateToMiniProgram;
    // canIUse检查openEmbeddedMiniProgram在此场景是否可用

    if (Taro.canIUse('openEmbeddedMiniProgram')) {
      openMiniProgram = Taro.openEmbeddedMiniProgram
    }

    openMiniProgram({
      appId: 'wx511b70ac7fda9822',
      path: `/pages/pagesLogistics/logisticsOrder/userOrder/friendPay?id=${id}&miniType=JMS`,
      envVersion: 'trial',
      success: (res) => {
        console.log(res, '拉去成功')
      },
      fail: (err) => {
        console.log(err, '拉去失败')
      }
    })
  }
  return {
    handleReceiptType,
    openEmbeddedMiniProgram
  }
}
